package cn.nextProject.service.impl;

import cn.nextProject.entity.RoleGroup;
import cn.nextProject.mapper.RoleGroupMapper;
import cn.nextProject.service.RoleGroupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class RoleGroupServiceImpl implements RoleGroupService {

    @Autowired
    private RoleGroupMapper roleGroupMapper;

    @Override
    @Cacheable(cacheNames = "roleGroups",
            key = "(#roleGroup.roleGroupName != null ? #roleGroup.roleGroupName : '') + '_' + " +
                    "#pageNum + '_' + #pageSize"
    )
    public PageInfo<RoleGroup> findByRoleGroup(RoleGroup roleGroup, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RoleGroup> byRoleGroup = roleGroupMapper.findByRoleGroup(roleGroup);
        PageInfo<RoleGroup> roleGroupPageInfo = new PageInfo<>(byRoleGroup);
        return roleGroupPageInfo;
    }

    @CacheEvict(cacheNames = {"roleGroups","roleGroup"},allEntries = true,beforeInvocation = true)
    @Override
    public void save(RoleGroup roleGroup) {
        roleGroupMapper.save(roleGroup);
    }

    @Cacheable(cacheNames = "roleGroup", key = "#roleGroupId")
    @Override
    public RoleGroup findOne(Long roleGroupId) {
        RoleGroup roleGroup = roleGroupMapper.findOne(roleGroupId);
        return roleGroup;
    }

    @CacheEvict(cacheNames = {"roleGroups","roleGroup","user"},allEntries = true,beforeInvocation = true)
    @Override
    public void update(RoleGroup roleGroup) {
        roleGroupMapper.update(roleGroup);
    }

    @CacheEvict(cacheNames = {"roleGroups","roleGroup","user"},allEntries = true,beforeInvocation = true)
    @Override
    public void delete(Long roleGroupId) {
        roleGroupMapper.delete(roleGroupId);
    }


}
